<?
include_once('conn.php');
include_once('include/func.php');
include("admin_check.php") ;
if($admin[0][2]<>10 and $admin[0][2]<>9){exit('<script>alert("您没有权限"); window.history.back(-1);</script>');}
if(isset($_GET[add])){
	$result = $db->INSERT("INSERT INTO `".tablepre."stock_type` (`name`, `rank`,`father`) VALUES ('$_POST[name]', '$_POST[rank]','$_POST[father]')");
	header("Location: $nowpage?ok");
	exit;
	}
//添加分类
if(isset($_GET[change])){
	$check = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where tid = '$_GET[change]'");
	
	
$updatedate = $db->UPDATE("UPDATE `".tablepre."stock_type` SET `name`='$_POST[name]',`rank`='$_POST[rank]',`father`='$_POST[father]' WHERE (`tid`='$_GET[change]')");
		header("Location: $nowpage?ch=$_GET[change]&ok");
	exit;	
	}
//修改分类
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? echo systemname;?></title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<?
if($_POST[ch]==1){
	foreach ($_POST[rank] as $key => $value) {
		$updatedate = $db->UPDATE("UPDATE `".tablepre."stock_type` SET `rank`='$value' WHERE (`tid`='$key')");
		}
			header("Location: $nowpage?ok");
	exit;	
	}elseif($_POST[ch]==2){
	foreach ($_POST[del] as $value) {
		$check = $db->select("SELECT father,name FROM `".tablepre."stock_type` where father = '$value'");
		if($check[0][0]<>null){
			$return_name = prefather($check[0][0]);
			echo '
		<script> 
function rt() 
{ 
  alert("'.htmlspecialchars_decode($return_name, ENT_QUOTES).'分类下有子分类，请先删除所有子分类！");
  return false(); 
} 
rt();
</script>
<script language=javascript>history.go(-1);</script>
';
exit;
}else{
	$result = $db->INSERT("DELETE FROM `".tablepre."stock_type` WHERE (`tid`='$value')");
}
}

	//$result = $db->INSERT("DELETE FROM `".tablepre."stock_type` WHERE (`tid`='9') LIMIT 1");
	header("Location: $nowpage?ok");
	exit;	
	//print_r($_POST[del]);
	}
	//删除分类
?>	
	<!-- jquery packed -->
	<script type="text/javascript" src="js/jquery-1.4.4.js"></script>
    <script type="text/javascript" src="js/dtree.js"></script>
	<!-- tableRowCheckboxToggle -->
	<script type="text/javascript" src="js/tableRowCheckboxToggle.js"></script>
    <script type="text/javascript"> 
    function fade() 
{ 
    //停留3秒后消失,在停留的时候,透明度为1.0什么都不做 
    $('<div id="add" class="quick-alert">操作成功</div>').insertAfter($("#example")).animate({opacity: 1.0}, 3000).fadeOut("slow",function(){ 
   //隐藏时把元素删除 
   $(this).remove(); 
}); 
} 
</script> 
</head>
<body <? echo isset($_GET[ok])?'onload="fade();"':''; ?>>
<div id="wrap">
<div id="main" class="clearfix">
<div id="side">
  </div>
  <div id="head"><? include_once('admin_head.php');?></div>
  <div id="left"><?
  include_once('admin_menu.php');
  ?></div>
  <div id="right"><img src="images/ttfjpg.php?t=分类管理&amp;bg=titlebg2.jpg&amp;z=18&amp;x=5&amp;y=22&amp;r=0&amp;b=0&amp;g=0"  /><br />

  <?
if(isset($_GET[ch])){
	$checkch = $db->select("SELECT * FROM `".tablepre."stock_type` where tid = '$_GET[ch]'");
	}
	
?>

<?
if(module_check(parameters(1))==null){
?>
<img src="images/ttfjpg.php?t=<? echo !isset($_GET[ch])?'添加分类':'修改分类';?>&amp;bg=titlebg2.jpg&amp;z=12&amp;x=5&amp;y=22&amp;r=96&amp;b=96&amp;g=96"  />
<form id="form1" name="form1" method="post" action="<? echo isset($_GET[ch])?'?change='.$_GET[ch]:'?add'; ?>">
  <table width="500" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align="right"></td>
      <td><div id="example"> </div> <? echo isset($_GET[ch])?'<a href="'.$nowpage.'">添加新分类</a>':''; ?></td>
    </tr>
    <tr>
      <td align="right">分类名： </td>
      <td><input type="text" name="name" value="<? echo $checkch[0][1]; ?>"  /></td>
    </tr>
    <tr>
      <td align="right">分类排序：</td>
      <td><input name="rank" type="text" value="<? echo isset($_GET[ch])?$checkch[0][2]:'0'; ?>" onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" /></td>
    </tr>
    <tr>
      <td align="right">上级目录：</td>
      <td><select name="father" >
      <?
	  if(isset($_GET[ch])){
		  if($checkch[0][3]==0){echo '<option value="0">主目录</option>';}else{
			  echo '<option value="'.$checkch[0][3].'">'.prefather($checkch[0][3]).'</option>';
			  }
		  }elseif(isset($_GET[add_sub])){
			  echo '<option value="'.$_GET[add_sub].'">'.prefather($_GET[add_sub]).'</option>';
			  }else{
			  echo '<option value="0">主目录</option>';
			  }
	  $arr = $db->select("SELECT tid,name,father FROM `".tablepre."stock_type`  order by tid");
      function fenlei($f_id=0,$lever=1)   
{   
    global $arr;   
    if($f_id == 0)    
        $lever=1;   
    else   
        $lever +=1;   
    for($i=0;$i<count($arr);$i++)   
    {   
        if($arr[$i][2]==$f_id)   
        {   
			echo '<option value="'.$arr[$i][0].'">'.str_repeat("-",$lever-1).$arr[$i][1].'</option>'; 
            fenlei($arr[$i][0],$lever);   
        }      
    }      
}  

echo fenlei();
	  ?></select></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="提交" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
<img src="images/ttfjpg.php?t=分类列表&amp;bg=titlebg2.jpg&amp;z=12&amp;x=5&amp;y=22&amp;r=96&amp;b=96&amp;g=96"  />
<form id="form2" name="form2" method="post" action="">
<table border="0" cellspacing="0" cellpadding="0" class="dataTable">
  <thead>
<tr>
<th class="dataTableHeader">选择</th>
<th class="dataTableHeader">名称</th>
<th class="dataTableHeader">添加商品</th>
<th class="dataTableHeader">查看本类商品</th>
<th class="dataTableHeader">排序</th>
<th class="dataTableHeader" style="white-space: nowrap">操作</th>
</tr>
</thead>
<tbody>
<?
$arr_list = $db->select("SELECT * FROM `".tablepre."stock_type` order by rank");
      function fenlei_list($f_id=0,$lever=1)   
{   
    global $arr_list;   
    if($f_id == 0)    
        $lever=1;   
    else   
        $lever +=1;   
    for($i=0;$i<count($arr_list);$i++)   
    {   
        if($arr_list[$i][3]==$f_id)   
        {   
		if($arr_list[$i][3]==0){$classcolor='odd_row';}elseif($arr_list[$i][3]==1){$classcolor='st1_row';}elseif($arr_list[$i][3]==2){$classcolor='st2_row';}elseif($arr_list[$i][3]==3){$classcolor='st3_row';}elseif($arr_list[$i][3]==4){$classcolor='st4_row';}else{$classcolor='even_row';}
		//$classcolor=$arr_list[$i][3]==0?'odd_row':'even_row';
		$leverno=$lever==1?'':'['.$lever.'级子类]';
			echo '<tr class="'.$classcolor.'">
<td><input type="checkbox" id="checkme1" name="del[]" value="'.$arr_list[$i][0].'" /></td>
<td>'.$leverno.''.str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",$lever-1).$arr_list[$i][1].'</td>
<td><a href="admin_stock_add.php?sid='.$arr_list[$i][0].'&tid='.$arr_list[$i][3].'">添加商品</a></td>
<td><a href="admin_stock_list.php?tid='.$arr_list[$i][0].'">查看商品列表</a></td>
<td style="white-space: nowrap"><input type="text" name="rank['.$arr_list[$i][0].']" value="'.$arr_list[$i][2].'" size="2" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))" /></td>
<td><a href="?ch='.$arr_list[$i][0].'">修改</a> | <a href="?add_sub='.$arr_list[$i][0].'">添加子目录</a></td>
</tr>
';
			//echo '<option value="'.$arr[$i][0].'">'.str_repeat("-",$lever-1).$arr[$i][1].'</option>'; 
            fenlei_list($arr_list[$i][0],$lever);   
        }      
    }      
}  

			echo fenlei_list();
?>
</tbody>
</table>
<select name="ch">
  <option value="1">修改</option>
  <option value="2">删除</option>
</select>
<input type="submit" name="button2" id="button2" value="提交" />
</form><? }else{
	echo '<font size="+2">已经与'.module_check(parameters(1)).'同步，不能独立修改！</a>';
	} ?></div>
  </div>
  </div>

<div id="footer">
<?
include_once('fooder.php');
?>
</div>
</BODY>
</HTML>
